package com.longrise.co.api;

import android.content.Context;
import android.util.Log;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.loaddata.newloaddata.LoadDataManager4;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.ly.common.commonutils.JsonUtils;

import java.lang.reflect.Type;

import rx.Observable;
import rx.Subscriber;

/**
 * Created by luoyang on 2017-6-22.
 */

public class ApiService {

    private static ApiService instance = null;

    private LoadDataManager4 loadDataManager = null;

    public static ApiService getInstance() {
        if(instance == null) {
            synchronized (ApiService.class) {
                if(instance == null) {
                    instance = new ApiService();
                }
            }
        }
        return instance;
    }

    private ApiService(){}

    public void init(Context context) {
        loadDataManager = LoadDataManager4.getInstance(context);
        //请求类型 1:GET 2:POST
        loadDataManager.setRequestType(2);
        //请求时没有网络时内部是否弹出提示
        loadDataManager.setShowNetNotice(true);
        loadDataManager.setCacheDic(context.getExternalCacheDir().getPath());
        //设置连接超时时间
        loadDataManager.setConnectTimeOut(20 * 1000);
        //设置读取数据超时时间
        loadDataManager.setSoTimeOut(20 * 1000);
        //设置返回code
        loadDataManager.setRestateKey("code");
    }

    public Observable<Object> request(final Type type, final EntityBean request, final String serviceName) {
        Log.i(serviceName, "request para: " + request);
        return Observable.create(new Observable.OnSubscribe<Object>(){
            @Override
            public void call(final Subscriber<? super Object> subscriber) {
                if(loadDataManager != null) {
                    String key = loadDataManager.getKey();
                    loadDataManager.setRestateKey("code");
                    loadDataManager.callService(key, ApiConstants.baseUrl, serviceName, request, new LoadDataManagerFather.OnRequestCompleteListener() {
                            @Override
                            public void onSuccess(String key, String service, Object result) {
                                try {
                                    if(result != null && result instanceof String) {
                                        Log.i(serviceName, (String) result);
                                        subscriber.onNext(JsonUtils.fromJson((String) result, type));
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }

                            }

                        @Override
                        public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                            subscriber.onError(new Throwable("请求失败"));
                        }

                        @Override
                        public void onFinished(String key, String service) {
                            subscriber.onCompleted();
                        }
                    });
                }
            }
        });
    }

    public Observable<Object> kckpRequest(final Type type, final EntityBean request, final String serviceName) {
        Log.i(serviceName, "request para: " + request);
        return Observable.create(new Observable.OnSubscribe<Object>(){
            @Override
            public void call(final Subscriber<? super Object> subscriber) {
                if(loadDataManager != null) {
                    String key = loadDataManager.getKey();
                    loadDataManager.setRestateKey("restate");
                    String url = CommonPara.getInstance().getKckpUrl();
                    loadDataManager.callService(key, url, serviceName, request, new LoadDataManagerFather.OnRequestCompleteListener() {
                        @Override
                        public void onSuccess(String key, String service, Object result) {
                            try {
                                if(result != null && result instanceof String) {
                                    Log.i(serviceName, (String) result);
                                    subscriber.onNext(JsonUtils.fromJson((String) result, type));
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                            subscriber.onError(new Throwable("请求失败"));
                        }

                        @Override
                        public void onFinished(String key, String service) {
                            subscriber.onCompleted();
                        }
                    });
                }
            }
        });
    }


    public Observable<String> kckpUploadPhotoRequest(final EntityBean request, final String serviceName) {
        Log.i(serviceName, "request para: " + request);
        return Observable.create(new Observable.OnSubscribe<String>(){
            @Override
            public void call(final Subscriber<? super String> subscriber) {
                if(loadDataManager != null) {
                    String key = loadDataManager.getKey();
                    loadDataManager.setRestateKey("restate");
                    String url = CommonPara.getInstance().getKckpUrl();
                    loadDataManager.callService(key, url, serviceName, request, new LoadDataManagerFather.OnRequestCompleteListener() {
                        @Override
                        public void onSuccess(String key, String service, Object result) {
                            try {
                                if(result != null && result instanceof String) {
                                    Log.i(serviceName, (String) result);
                                    subscriber.onNext((String)result);
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {
                            subscriber.onError(new Throwable("请求失败"));
                        }

                        @Override
                        public void onFinished(String key, String service) {
                            subscriber.onCompleted();
                        }
                    });
                }
            }
        });
    }
}
